// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-15

package record

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib"
)

// Logic
// 场次购买分页.
type Logic struct {
	arcade   *models.Arcades
	admin    *models.ArcadeAdmins
	request  *Request
	response []*Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: make([]*Response, 0),
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Query(i, o.request); err == nil {
		o.arcade, _ = lib.Arcade.GetMaster().GetArcade(i)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		conditions = map[string]any{"session_id": o.request.Id}
		err        error
		list       []*models.TicketRecord
		listSeat   = make([]int64, 0)
		listUser   = make([]kind.UserId, 0)
		res        = result.New()
		service    = services.NewTicketRecordService()
		total      int64
	)

	// 1. 读取列表.
	if list, total, err = service.Paging(ctx, conditions, o.request.Page, o.request.Size); err != nil {
		return res.WithError(errs.ErrDatabaseQuery)
	}

	// 2. 遍历列表.
	for _, item := range list {
		// 2.1 用户列表.
		if item.UserId.IsBound() {
			listUser = append(listUser, item.UserId)
		}
		// 2.2 钓位列表.
		if item.SeatId > 0 {
			listSeat = append(listSeat, item.SeatId)
		}
		// 2.3 结果列表.
		o.response = append(o.response, (&Response{}).With(item))
	}

	// 3. 绑定钓位.
	if len(listSeat) > 0 {
		if err = o.RunSeat(ctx, listSeat); err != nil {
			return res.WithError(err)
		}
	}

	// 4. 绑定用户.
	if len(listUser) > 0 {
		// 4.1 钓友买家.
		if err = o.RunUser(ctx, listUser); err != nil {
			return res.WithError(err)
		}
		// 4.2 会员钓友.
		if err = o.RunMember(ctx, listUser); err != nil {
			return res.WithError(err)
		}
	}

	// 5. 加载列表.
	return res.WithPaging(o.response, total, o.request.Page, o.request.Size)
}

func (o *Logic) RunMember(ctx context.Context, list []kind.UserId) (err error) {
	var mapper = make(map[kind.UserId]*models.ArcadeMember)

	// 1. 读取列表.
	if mapper, err = services.NewArcadeMemberService().MapByIdList(ctx, o.arcade.Id, list); err != nil {
		return
	}

	// 2. 附加用户.
	for _, v := range o.response {
		if v.UserId.IsBound() {
			if u, ok := mapper[v.UserId]; ok {
				v.WithMember(u)
			}
		}
	}
	return
}

// RunSeat
// 处理钓位.
func (o *Logic) RunSeat(ctx context.Context, list []int64) (err error) {
	var mapper = make(map[int64]*models.ArcadePondSeat)

	// 1. 读取列表.
	if mapper, err = services.NewArcadePondSeatService().MapByIdList(ctx, list); err != nil {
		return
	}

	// 2. 附加用户.
	for _, v := range o.response {
		if v.SeatId > 0 {
			if u, ok := mapper[v.SeatId]; ok {
				v.WithSeat(u)
			}
		}
	}
	return
}

// RunUser
// 处理用户.
func (o *Logic) RunUser(ctx context.Context, list []kind.UserId) (err error) {
	var mapper = make(map[kind.UserId]*models.Users)

	// 1. 读取列表.
	if mapper, err = services.NewUsersService().MapByIdList(ctx, list); err != nil {
		return
	}

	// 2. 附加用户.
	for _, v := range o.response {
		if v.UserId.IsBound() {
			if u, ok := mapper[v.UserId]; ok {
				v.WithUser(u)
			}
		}
	}
	return
}
